package com.xunkids.bos.migration.component.oss;

import com.alibaba.fastjson2.JSON;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.Credentials;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;

import com.aliyun.oss.model.*;
import com.aliyuncs.exceptions.ClientException;
import com.xunkids.bos.migration.common.constant.BusinessConstant;

import java.io.InputStream;


/**
 * @program: bos-migration
 * @author: byl
 * @create: 2024-04-02 15:09
 * @Description
 */
public class OSStorage {



    private static class SingletonHolder{
        private static final OSStorage instance = new OSStorage();

    }

    //获取单例的方法
    public static  OSStorage getInstance(){
        return OSStorage.SingletonHolder.instance;
    }


    private static OSSClient ossClient;


    private OSStorage() {
    }


    /**
     * 获取oss client
     * @return
     */
    public OSS getOssClient() {


        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        DefaultCredentialProvider credentialsProvider = CredentialsProviderFactory.newDefaultCredentialProvider
                (BusinessConstant.XIAOMI_ACCESS_KEY_ID,BusinessConstant.XIAOMI_ACCESS_KEY_SECRET);
        // 创建OSSClient实例。
        return new OSSClientBuilder().build(BusinessConstant.XIAOMI_END_POINT, credentialsProvider);
    }


    /**
     * 获取oss client
     * @return
     */
    public OSS getOssTestClient() {
        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        DefaultCredentialProvider credentialsProvider = CredentialsProviderFactory.newDefaultCredentialProvider
                (BusinessConstant.XIAOMI_ACCESS_TEST_KEY_ID,BusinessConstant.XIAOMI_ACCESS_TEST_KEY_SECRET);
        // 创建OSSClient实例。
        return new OSSClientBuilder().build(BusinessConstant.XIAOMI_END_POINT, credentialsProvider);
    }




    /**
     * @param bucketName 创建得bucketName
     * @return 创建bucket
     */
    public String buildBucket(String bucketName) {
        OSS ossClient = getOssClient();
        try {
            // 创建CreateBucketRequest对象。
            CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);

            // 如果创建存储空间的同时需要指定存储类型、存储空间的读写权限、数据容灾类型, 请参考如下代码。
            // 此处以设置存储空间的存储类型为标准存储为例介绍。
            createBucketRequest.setStorageClass(StorageClass.Standard);
            // 数据容灾类型默认为本地冗余存储，即DataRedundancyType.LRS。如果需要设置数据容灾类型为同城冗余存储，请设置为DataRedundancyType.ZRS。
//            createBucketRequest.setDataRedundancyType(DataRedundancyType.ZRS);
            // 设置存储空间读写权限为公共读，默认为私有。
//            createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
            // 在支持资源组的地域创建Bucket时，您可以为Bucket配置资源组。
//            createBucketRequest.setResourceGroupId(rsId);
            // 创建存储空间。
           return JSON.toJSONString(ossClient.createBucket(createBucketRequest));
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }


    public PutObjectResult putObject(String bucketName , String objectName , InputStream inputStream) throws ClientException {
        OSS ossClient = getOssClient();
        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 创建PutObject请求。
            return ossClient.putObject(putObjectRequest);

        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }




    public VoidResult deleteObject(String bucketName , String objectName ) throws ClientException {
        OSS ossClient = getOssClient();
        try {
            // 创建PutObjectRequest对象。
            return ossClient.deleteObject(bucketName, objectName);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
